package net.huashitong.node.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;

import net.huashitong.node.entity.NodeSchedules;

@Repository
public class NodeSchedulesDao extends StringDao<NodeSchedules> {
	
	public List<NodeSchedules> queryNodeSchedules(){
		GenericQuery query = createQuery("obj");
		return query.listResult();
	}
	public List<NodeSchedules> queryNodeSchedulesByProId(String projectId){
		GenericQuery query = createQuery("obj");
		query.append(" and obj.projectId = :proId").setParam("proId", projectId);
		return query.listResult();
	}
	public NodeSchedules getNOdeSchedule(String projectId,String nodeId){
		GenericQuery query = createQuery("obj");
		query.append(" and obj.projectId = :projectId").setParam("projectId", projectId);
		query.append(" and obj.nodeId = :nodeId").setParam("nodeId", nodeId);
		return query.singleResult();
	}
	public List<NodeSchedules> getNodesByPhone(String phone){
		GenericQuery query = createQuery("obj");
		//query.append(" and (obj.operatorPhone = :operatorPhone or obj.resUserPhone = :resUserPhone)").setParam("operatorPhone", phone).setParam("resUserPhone", phone);
		query.append(" and (obj.operatorPhone = :operatorPhone)").setParam("operatorPhone", phone);
		return query.listResult();
	}
	public List<NodeSchedules> getChlidNodes(String projectId,String parentNodeId){
		GenericQuery query = createQuery("obj");
		query.append(" and obj.projectId = :projectId").setParam("projectId", projectId);
		query.append(" and obj.nodeId like :nodeId").setParam("nodeId", parentNodeId+"%");
		return query.listResult();
	}
	public List<NodeSchedules> getNodesByIds(String[] nodeIds,String projectId){
		GenericQuery query = createQuery("obj");
		query.append(" and obj.projectId = :projectId").setParam("projectId", projectId);
		query.append(" and obj.nodeId in (:ids)").setParamList("ids", nodeIds);
		return query.listResult();
	}
	public List<NodeSchedules> getUnCheckNodes(String phone){
		GenericQuery query = createQuery("obj");
		query.append(" and obj.operatorPhone = :operatorPhone").setParam("operatorPhone", phone);
		query.append(" and obj.isCheck = :isCheck").setParam("isCheck", 0);
		return query.listResult();
	}
	public void setNodesCheck(String phone){
		String hql = "update NodeSchedules set isCheck=:isCheck where operatorPhone=:operatorPhone";
		Session session = sessionFactory.openSession();
		Query query = session.createQuery(hql);
		query.setParameter("isCheck", 1);
		query.setParameter("operatorPhone", phone);
		query.executeUpdate();
	}
	public List<NodeSchedules> getNodeSchedulesByTypeAndId(String projectId,String type){
		GenericQuery query = createQuery("obj");
		query.append(" and obj.projectId = :projectId").setParam("projectId", projectId);
		query.append(" and obj.type =:type").setParam("type",type);
		return query.listResult();
	}
	
	
}
